<?php

namespace Index\Controller;

use Think\Controller;
use Index\ViewModel;
use Index\Service;
use Think\Model;

/**
 * 说明：反馈管理控制器
 * 作者：王楠
 * 创建日期：2017-09-15
 */
class FeedbackController extends CommonController
{

    /**
     * 反馈列表
     */
    public function feedbacklist()
    {
        $app_code = $this->app_code;
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $appcode = trim(I('ddlapp'));

	if (!empty($app_code)) {
	    $map['app_flag'] = $app_code;
	} else {
	    if (!empty($appcode)) {
		$map['app_flag'] = $appcode;
	    }
	}
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($keyword)) {
            $where['chat_feedback.id'] = array('eq', $keyword);
            $where['chat_feedback.content'] = array('like', '%' . $keyword . '%');
            $where['chat_userinfo.user_id'] = array('eq', $keyword);
            $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
            $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $viewModel = new ViewModel\FeedbackViewModel();
        $total = $viewModel->where($map)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();
        $field = array(
            'id', 'content', 'img', 'user_id', 'user_logo', 'user_name', 'user_phone', 'ctime'
        );
        $data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order('ctime desc')->select();
        foreach ($data as $key => $value) {
            $data[$key]['img'] = empty($data[$key]['img']) ? '' : explode(',', $data[$key]['img'])[0];
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
            $user_info = $this->app_name2.'号：' . $data[$key]['user_id'] . '<br />用户昵称：' . $data[$key]['user_name'] . '<br />手机号码：' . $data[$key]['user_phone'];
            $data[$key]['user_info'] = $user_info;
        }
        $search = array('appsel' => $appcode, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'search' => $search,
            'app_code' => $app_code,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }

    /**
     * 反馈详情
     */
    public function feedbackdetail()
    {
        $sex_enum = getenumbykey('sex');
        $id = I('id');
        $viewModel = new ViewModel\FeedbackViewModel();
        $field = [
            'id', 'content', 'img', 'user_id', 'user_logo', 'user_name', 'user_sex', 'user_phone', 'ctime'
        ];
        $fb = $viewModel->where(array('id' => $id))->field($field)->find();
        if ($fb) {
            $fb['user_sex_text'] = empty($fb['user_sex']) ? $sex_enum['3'] : $sex_enum[$fb['user_sex']];
            $fb['ctime'] = date('Y-m-d H:i', $fb['ctime']);
            $fb['imgs'] = empty($fb['img']) ? '' : explode(",", $fb['img']);
        }
        $viewModel = new ViewModel\FeedbackreplyViewModel();
        $field = [
            'id', 'content', 'username', 'name', 'ctime'
        ];
        $data = $viewModel->where(array('fb_id' => $id))->field($field)->order('ctime desc')->select();
        foreach ($data as $key => $value) {
            $data[$key]['admin_name_text'] = empty($data[$key]['name']) ? $data[$key]['username'] : $data[$key]['name'];
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
        }
        $this->assign(array(
            'id' => $id,
            'fb' => $fb,
            'data' => $data,
        ));
        $this->display();
    }

    /**
     * 回复弹框
     */
    public function replyfeedback()
    {
        $id = I('id');
        $this->id = $id;
        $this->display();
    }

    /**
     * 回复提交
     */
    public function replyfeedbackfunc()
    {
        $admin_id = $this->admin_id;
        $id = I('id');
        $content = I('content');
        $viewModel = new ViewModel\FeedbackViewModel();
        $data = $viewModel->where(array('id' => $id))->find();
        if (!empty($data)) {
            $user_id = $data['user_id'];
            $this->feedbackreplysend($id, $user_id, $admin_id, $content); //发送回复消息
        } else {
            $this->ajaxReturn(array('code' => '2', 'result' => '获取数据失败'));
        }
    }

    /**
     * 回复发送
     * @param type $user_id
     * @param type $content
     */
    public function feedbackreplysend($id, $user_id, $admin_id, $content)
    {
        $app_code = $this->app_code;
        if (empty($user_id) || empty($content)) {
            $this->ajaxReturn(array('code' => '2', 'result' => '回复失败，请重试'));
            exit();
        }
        $appKey = 'y745wfm8y1smv';
        $appSecret = '9Jf4l94jzLcE';
        $RongCloud = new \Index\Service\RongCloud($appKey, $appSecret);
        //$arr['operation'] =  'op1';
        //$arr['sourceUserId'] =  '1';
        //$arr['targetUserId'] =  I('uid2');
        $arr['content'] = $content;
        $arr['extra'] = 'helloExtra';
        $string = json_encode($arr);
        $result = $RongCloud->Message()->publishPrivate('1', $user_id, 'RC:TxtMsg', $string, '', '', '', '0', '1', '1');
        $result = json_decode($result, true);
        if ($result['code'] == '200') {
            $data['admin_id'] = $admin_id;
            $data['fb_id'] = $id;
            $data['content'] = $content;
            $data['ctime'] = time();
            $data['app_flag'] = $app_code;
            $chat_feedback_re = M('chat_feedback_re');
            $chat_feedback_re->add($data);
            $this->ajaxReturn(array('code' => '1', 'result' => '回复成功'));
            exit();
        } else {
            $this->ajaxReturn(array('code' => '2', 'result' => '回复失败，请重试'));
            exit();
        }
    }

    /**
     * 回复列表
     */
    public function feedbackreplylist()
    {
        $app_code = $this->app_code;
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $appcode = trim(I('ddlapp'));
	if (!empty($app_code)) {
	    $map['app_flag'] = $app_code;
	} else {
	    if (!empty($appcode)) {
		$map['app_flag'] = $appcode;
	    }
	}
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['chat_feedback_re.ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['chat_feedback_re.ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['chat_feedback_re.ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($keyword)) {
            $where['chat_feedback_re.id'] = array('eq', $keyword);
            $where['chat_feedback_re.content'] = array('like', '%' . $keyword . '%');
            $where['chat_feedback.content'] = array('like', '%' . $keyword . '%');
            $where['chat_admin.username'] = array('like', '%' . $keyword . '%');
            $where['chat_admin.name'] = array('like', '%' . $keyword . '%');
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $viewModel = new ViewModel\FeedbackreplyViewModel();
        $total = $viewModel->where($map)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();
        $field = [
            'id', 'fb_id', 'content', 'ctime', 'fb_img', 'fb_content', 'username', 'name'
        ];
        $data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order('ctime desc')->select();
        foreach ($data as $key => $value) {
            $data[$key]['admin_name_text'] = empty($data[$key]['name']) ? $data[$key]['username'] : $data[$key]['name'];
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
        }
        $search = array('appsel' => $appcode, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'search' => $search,
            'app_code' => $app_code,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }

    /**
     * 回复详情
     */
    public function feedbackreplydetail()
    {
        $id = I('id');
        $map['id'] = $id;
        $viewModel = new ViewModel\FeedbackreplyViewModel();
        $data = $viewModel->where($map)->find();
        if ($data) {
            $data['imgs'] = empty($data['fb_img']) ? '' : explode(",", $data['fb_img']);
            $data['fb_ctime'] = date('Y-m-d H:i', $data['fb_ctime']);
            $data['admin_name_text'] = empty($data['name']) ? $data['username'] : $data['name'];
            $data['ctime'] = date('Y-m-d H:i', $data['ctime']);
        }
        $this->assign(array(
            'id' => $id,
            'data' => $data,
        ));
        $this->display();
    }

    public function deletesubmit()
    {
        $id = I('id');
        if (!empty($id)) {
            $model = M('chat_feedback_re');
            $count = $model->where(array('fb_id' => $id))->count();
            if ($count) {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该反馈存在回复，请先删除回复记录！'));
                exit();
            }
            $model = M('chat_feedback');
            $result = $model->where(array('id' => $id))->delete();
            if ($result) {
                return $this->ajaxReturn(array('ret' => '1001', 'code' => '删除成功！'));
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '删除失败！'));
            }
        }
    }

    public function deletereplysubmit()
    {
        $id = I('id');
        if (!empty($id)) {
            $model = M('chat_feedback_re');
            $result = $model->where(array('id' => $id))->delete();
            if ($result) {
                return $this->ajaxReturn(array('ret' => '1001', 'code' => '删除成功！'));
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '删除失败！'));
            }
        }
    }

}
